﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class user_Choose : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count == 0)
        {
            GridView1_empty();
        }
    }




    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        String id = ((Button)sender).CommandArgument.ToString();
        int id_user = (int)Session["id"];
        SqlConnection sqlcon;
        SqlCommand sqlcom;

        String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        string sql1 = "select * from 选购 where 商品编号='" + id + "'and 客户编号 =" + id_user;
        sqlcon = new SqlConnection(strConnection);
        sqlcon.Open();
        sqlcom = new SqlCommand(sql1, sqlcon);
        SqlDataReader Dr1 = sqlcom.ExecuteReader();
        string sqlstr = "";
        try
        {
            if (Dr1.Read())
            {
                int count = Convert.ToInt32(Dr1["数量"].ToString()) + 1;
                sqlstr = "update 选购 set 数量=" + count + "where 商品编号 ='" + id + "'";

            }
            else
            {
                sqlstr = "insert into 选购 (商品编号,客户编号,数量) values ('" + id + "','" + id_user + "','1" + "')";
            }


        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message.ToString());
        }
        finally
        {
            Dr1.Close();
        }
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        sqlcon = new SqlConnection(strConnection);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        if (name.Length != 0)
        {
            SqlConnection sqlcon;
            SqlCommand sqlcom;

            String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            String sql = "SELECT 商品编号,图片,商品名称,价格 FROM 商品 where 数量!=0";
            sqlcon = new SqlConnection(strConnection);
            sqlcon.Open();
            sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader Dr = sqlcom.ExecuteReader();
            int a = 0;
            try
            {
                while (Dr.Read())
                {
                    string name1 = Dr["商品名称"].ToString();
                    if (name1.Contains(name))
                    {
                        string sql1 = "SELECT 商品编号,图片,商品名称,价格 FROM 商品 where 商品名称 like '%" + name + "%'";
                        GridView1.DataSourceID = "SqlDataSource1";
                        SqlDataSource1.SelectCommand = sql1;
                        
                        a = 1;
                        break;
                    }

                }
                if (a == 0)
                {
                    GridView1_empty();
                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                Dr.Close();
            }
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            //SqlDataSource1.Select(DataSourceSelectArguments.Empty);

        }
    }
    protected void GridView1_empty()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("商品编号");
        dt.Columns.Add("图片");
        dt.Columns.Add("商品名称");
        dt.Columns.Add("价格");
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSourceID = "";
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int columnCount = dt.Columns.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "暂无该商品记录！";
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
        }

    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
}